package com.bamboo.custom.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.bamboo.custom.act.BombActivity;
import com.bamboo.custom.act.RulerActivity;
import com.bamboo.custom.act.SunActivity;
import com.bamboo.custom.adapter.CommonAdapter;
import com.bamboo.custom.bean.CommonBean;
import com.bamboo.custom.databinding.FragmentHomeBinding;
import com.bamboo.custom.view.SunView;


import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {

    private FragmentHomeBinding binding;
    private CommonAdapter mAdapter;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        HomeViewModel homeViewModel =
                new ViewModelProvider(this).get(HomeViewModel.class);

        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        return root;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initRecyclerView();

    }

    private void initRecyclerView() {
         mAdapter=new CommonAdapter(getData());
         binding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
         binding.recyclerView.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL));
         binding.recyclerView.setAdapter(mAdapter);
         mAdapter.setOnItemClickListener((adapter, view, position) -> {
             CommonBean bean= (CommonBean) adapter.getItem(position);
             startActivity(new Intent(getContext(),bean.getClazz()));
         });
    }

    private List<CommonBean> getData() {
        List<CommonBean> data=new ArrayList<>();
        data.add(new CommonBean("尺子", RulerActivity.class));
        data.add(new CommonBean("萌萌炸弹效果", BombActivity.class));
        data.add(new CommonBean("天气小太阳", SunActivity.class));

        return data;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}